/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.menosehmais.controller.rest;

import br.com.caelum.vraptor.Get;
import static br.com.caelum.vraptor.view.Results.json;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.Validator;
import br.com.menosehmais.business.PortletBO;
import br.com.menosehmais.dto.PortletDTO;
import br.com.menosehmais.dto.VitrinaDTO;
import br.com.menosehmais.framework.presentation.AbstractRestController;
import br.com.menosehmais.framework.security.annotation.PermitAll;
import br.com.menosehmais.model.PortletDO;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Junior JAFJ
 */
@Resource
public class VitrinaController extends AbstractRestController {

    private PortletBO portletBO;
    
    public VitrinaController(HttpServletRequest request, Result result, Validator validator, PortletBO portletBO){
        super(request, result, validator);
        this.portletBO = portletBO;
    }

    @Get
    @PermitAll
    @Path("/rest/vitrina/carregarPortlets/")
    public void carregarPortlets() {
        Random r = new Random(System.currentTimeMillis());
        int tipoVitrina = 1 + (r.nextInt() % 3);
        VitrinaDTO vitrinaDTO = new VitrinaDTO();
        List<PortletDTO> itensPrateleira3 = new LinkedList<PortletDTO>();
        List<PortletDO> listaPortlets = portletBO.listarPortlets();

        if (!listaPortlets.isEmpty()) {
            switch(tipoVitrina) {
                case 1:
                    List<PortletDTO> itensPrateleira1 = new LinkedList<PortletDTO>();
                    List<PortletDTO> itensPrateleira2 = new LinkedList<PortletDTO>();

                    for (int i = 1; i < 4; i++) {
                        PortletDTO portletDTO = new PortletDTO();
                        portletDTO.setId(listaPortlets.get(i).getId());
                        portletDTO.setHint(listaPortlets.get(i).getHint());
                        portletDTO.setUrl("img/port" + i + ".png");
                        itensPrateleira1.add(portletDTO);

                        PortletDTO portletDTO2 = new PortletDTO();
                        portletDTO2.setId(listaPortlets.get(i+3).getId());
                        portletDTO2.setHint(listaPortlets.get(i+3).getHint());
                        portletDTO2.setUrl("img/port" + (i + 3) + ".png");
                        itensPrateleira2.add(portletDTO2);

                        PortletDTO portletDTO3 = new PortletDTO();
                        portletDTO3.setId(listaPortlets.get(i+6).getId());
                        portletDTO3.setHint(listaPortlets.get(i+6).getHint());
                        portletDTO3.setUrl("img/port" + (i + 6) + ".png");
                        itensPrateleira3.add(portletDTO3);
                    }

                    vitrinaDTO.setTipo(VitrinaDTO.Tipo.NORMAL);
                    vitrinaDTO.setPrateleira1(itensPrateleira1);
                    vitrinaDTO.setPrateleira2(itensPrateleira2);
                    vitrinaDTO.setPrateleira3(itensPrateleira3);
                    break;
                case 2:
                    vitrinaDTO.setTipo(VitrinaDTO.Tipo.HALF);
                    vitrinaDTO.getPrateleiraHalf().setId(listaPortlets.get(2).getId());
                    vitrinaDTO.getPrateleiraHalf().setHint(listaPortlets.get(2).getHint());
                    vitrinaDTO.getPrateleiraHalf().setUrl("img/portHalf1.png");
                    for (int i = 1; i < 4; i++) {
                        PortletDTO portletDTO = new PortletDTO();
                        portletDTO.setId(listaPortlets.get(i+6).getId());
                        portletDTO.setHint(listaPortlets.get(i+6).getHint());
                        portletDTO.setUrl("img/port" + (i + 6) + ".png");
                        itensPrateleira3.add(portletDTO);
                    }
                    vitrinaDTO.setPrateleira3(itensPrateleira3);
                    break;
                case 3:
                default:
                    vitrinaDTO.setTipo(VitrinaDTO.Tipo.FULL);
                    vitrinaDTO.getPrateleiraFull().setId(listaPortlets.get(5).getId());
                    vitrinaDTO.getPrateleiraFull().setHint(listaPortlets.get(5).getId());
                    vitrinaDTO.getPrateleiraFull().setUrl("img/portletFull-1.jpg");
                    break;
            }
        }
        result.use(json()).from(vitrinaDTO).recursive().serialize();
    }
}
